<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理  pdo_fetchall
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'排课列表'),
	//array('op'=>'next','title'=>'明年排课列表'),
);
$return['status']=$this->lesson_status;
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$area_id=intval($_RQ['area_id']);
	if(empty($area_id)){
		$area=pdo_get('jy_kexie_area',$params);
		$area_id=$area['id'];
		//exi('请选择区域！','error')
	}else{
		$params['id']=$area_id;
		$area=pdo_get('jy_kexie_area',$params);
	}
	$return['params']['area_id']=''.$area_id;
	$list=array();
	if(empty($_RQ['year'])){
		$year = date('Y');
	}else{
		$year=$_RQ['year'];
	}
	$return['params']['year']=$year;
	$basic=$this->plugin_setting('basic');
	$basic['on_months']=str_array($basic['on_months']);
	foreach($basic['on_months'] as $ov){
		$on_months[]=array('month'=>$ov);
	}
	paixu($on_months,'month','asc');
	foreach($on_months as $k=>$ov){
			$keyid=$year.$ov['month'];
			$list[$keyid]=array(
				'year'=>$year,
				'month'=>$ov['month'],
			);
	}
	$workers=array();
	$storeparams=array('area_id'=>$area_id,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['storeid'])){
		$storeparams['id']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	$listhtmls=array(
		array('name'=>'month','title'=>'月份','type'=>'string'),
	);
	$stores=pdo_getall('jy_kexie_store',$storeparams,array('id','title'));
	foreach($stores as $sv){
			$listhtmls[]=array(
				'name'=>'store_'.$sv['id'],
				'title'=>$sv['title'],
				'ishuan'=>1,
				'type'=>'string'
			);
			foreach($on_months as $k=>$ov){
					$keyid=$year.$ov['month'];
					$params=array('onday like'=>'%'.$year.'%','onmonth'=>$ov['month'],'storeid'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']);
					if(!empty($_RQ['title'])){
						$params['title like']='%'.trim($_RQ['title']).'%';
						$return['params']['title']=$_RQ['title'];
					}
					if(!empty($_RQ['groupid'])){
						$params['groupid']=intval($_RQ['groupid']);
						$return['params']['groupid']=$_RQ['groupid'];
					}
					if(!empty($_RQ['workerid'])){
						$params['workerid']=intval($_RQ['workerid']);
						$return['params']['workerid']=$_RQ['workerid'];
					}
					if($_RQ['ontime']){
						$time_range=cfc('tools')->base64_decode($_RQ['ontime'],$_SESSION['base_key_str']);
						$time_range=json_decode($time_range,true);
						$return['params']['ontime']=$time_range;
						$params['ontime between']=array($time_range['tstart'],$time_range['tend']);
					}
					$pais=pdo_getall('jy_kexie_lesson_pai',$params,array('id','workerid','onday'));
					$data='';
					paixu($pais,'workerid','asc');
					foreach($pais as $pv){
						$name=pdo_getcolumn('jy_kexie_worker',array('id'=>$pv['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
						$onday=date('d',strtotime($pv['onday']));
						$data .=$name.'（'.$onday.'）、';
					}
					$data=rtrim($data, "、");
					$list[$keyid]['store_'.$sv['id']]=$data;
			}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('total'=>$total,'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'tip'=>'[温馨提示]：由校区自己后台，添加排课！
		</br>1、数据多了，导出异常？联系管理设置PHP.INI配置！修改memory_limit大小！
		</br>2、只能同时显示一个区域的校区；
		','list'=>$listhtmls,'batch'=>array(
			array('type'=>'api','url'=>'/lesson_pai/export/area_type=1&year='.$year,'title'=>'按区域导出'),
			//array('type'=>'api','url'=>'/lesson_pai/export/store_type2=1','title'=>'按校区列表导出'),
			array('type'=>'api','url'=>'/lesson_pai/export/store_type=1&year='.$year,'title'=>'按校区汇总导出'),
			array('type'=>'api','url'=>'/lesson_pai/export/worker_type=1&year='.$year,'title'=>'按讲师导出'),
		)),
		'search'=>array(
			array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'storeid','width'=>10,'title'=>'所属校区','type'=>'select','options'=>'areas[pagedata.params.area_id].stores'),
			array('name'=>'groupid','width'=>10,'title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','width'=>10,'title'=>'讲师','type'=>'select_more','options'=>'workers','option_title'=>'name'),
			array('name'=>'title','width'=>10,'title'=>'课程名称','type'=>'string'),
			array('name'=>'year','width'=>10,'title'=>'所属年份','type'=>'select','options'=>'years'),
			array('type'=>'time_range','width'=>30,'name'=>'ontime','title'=>'上课时间'),
		)
	);
	$return['htmls']=$htmls;
	$return['years']=array();
	$nowyear = date('Y');
	for($i=1;$i<=10;$i++){
		$now=$nowyear-(10-$i);
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	for($i=1;$i<=2;$i++){
		$now=$nowyear+$i;
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['areas'] as &$av){
		$av['stores']=pdo_getall('jy_kexie_store',array('area_id'=>$av['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	}
	//$return['stores']=pdo_getall('jy_kexie_store',array('area_id'=>$area_id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		//$gv['workers']=pdo_getall('jy_kexie_worker',array('lesson_groupid'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	}
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='next'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$area_id=intval($_RQ['area_id']);
	if(empty($area_id)){
		$area=pdo_get('jy_kexie_area',$params);
		$area_id=$area['id'];
		//exi('请选择区域！','error')
	}
	$return['params']['area_id']=$area_id;
	$list=array();
	$year = date('Y')+1;
	$basic=$this->plugin_setting('basic');
	$basic['on_months']=str_array($basic['on_months']);
	foreach($basic['on_months'] as $ov){
		$on_months[]=array('month'=>$ov);
	}
	paixu($on_months,'month','asc');
	foreach($on_months as $k=>$ov){
			$keyid=$year.$ov['month'];
			$list[$keyid]=array(
				'year'=>$year,
				'month'=>$ov['month'],
			);
	}
	$workers=array();
	$storeparams=array('area_id'=>$area_id,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['storeid'])){
		$storeparams['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	$listhtmls=array(
		array('name'=>'month','title'=>'月份','type'=>'string'),
	);
	$stores=pdo_getall('jy_kexie_store',$storeparams,array('id','title'));
	foreach($stores as $sv){
			$listhtmls[]=array(
				'name'=>'store_'.$sv['id'],
				'title'=>$sv['title'].'('.$area['title'].')',
				'ishuan'=>1,
				'type'=>'string'
			);
			foreach($on_months as $k=>$ov){
					$keyid=$year.$ov['month'];
					$params=array('onday like'=>'%'.$year.'%','onmonth'=>$ov['month'],'storeid'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']);
					if(!empty($_RQ['title'])){
						$params['title like']='%'.trim($_RQ['title']).'%';
						$return['params']['title']=$_RQ['title'];
					}
					if(!empty($_RQ['groupid'])){
						$params['groupid']=intval($_RQ['groupid']);
						$return['params']['groupid']=$_RQ['groupid'];
					}
					if(!empty($_RQ['workerid'])){
						$params['workerid']=intval($_RQ['workerid']);
						$return['params']['workerid']=$_RQ['workerid'];
					}
					if($_RQ['ontime']){
						$time_range=cfc('tools')->base64_decode($_RQ['ontime'],$_SESSION['base_key_str']);
						$time_range=json_decode($time_range,true);
						$return['params']['ontime']=$time_range;
						$params['ontime between']=array($time_range['tstart'],$time_range['tend']);
					}
					
					$pais=pdo_getall('jy_kexie_lesson_pai',$params,array('id','workerid','onday'));
					$data='';
					paixu($pais,'workerid','asc');
					foreach($pais as $pv){
						$name=pdo_getcolumn('jy_kexie_worker',array('id'=>$pv['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
						$onday=date('d',strtotime($pv['onday']));
						$data .=$name.'（'.$onday.'）、';
					}
					$data=rtrim($data, "、");
					$list[$keyid]['store_'.$sv['id']]=$data;
			}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('total'=>$total,'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'tip'=>'[温馨提示]：由校区自己后台，添加排课！
		</br>1、数据多了，导出异常？联系管理设置PHP.INI配置！修改memory_limit大小！
		</br>2、只能同时显示一个区域的校区；
		','list'=>$listhtmls,'batch'=>array(
			array('type'=>'api','url'=>'/lesson_pai/export/area_type=1','title'=>'[明年]按区域导出'),
			//array('type'=>'api','url'=>'/lesson_pai/export/store_type2=1','title'=>'按校区列表导出'),
			array('type'=>'api','url'=>'/lesson_pai/export/store_type=1','title'=>'[明年]按校区汇总导出'),
			array('type'=>'api','url'=>'/lesson_pai/export/worker_type=1','title'=>'[明年]按讲师导出'),
		)),
		'search'=>array(
			array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'storeid','width'=>10,'title'=>'所属校区','type'=>'select_more','options'=>'stores'),
			array('name'=>'groupid','width'=>10,'title'=>'课程类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','width'=>10,'title'=>'讲师','type'=>'select_more','options'=>'workers','option_title'=>'name'),
			array('name'=>'title','width'=>10,'title'=>'课程名称','type'=>'string'),
			array('type'=>'time_range','width'=>30,'name'=>'ontime','title'=>'上课时间'),
		)
	);
	$return['htmls']=$htmls;
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['areas'] as &$av){
		//$av['stores']=pdo_getall('jy_kexie_store',array('area_id'=>$av['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	}
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		//$gv['workers']=pdo_getall('jy_kexie_worker',array('lesson_groupid'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	}
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['ontime']=date('Y-m-d H:i:s',$item['ontime']);
		$item['lesson']=pdo_get('jy_kexie_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']));
			$item['endtime']=$this->model('lesson')->get_endtime($item['ontime'],$item['lesson']['on_num'],$item['workerid']);
			$item['ontime']=$this->model('lesson')->get_uptime($item['ontime']);
			$item['worker']=pdo_get('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回课程列表'),
		),
		'look'=>array(
					array('name'=>'lesson.title','title'=>'课程名称','type'=>'string'),
					array('name'=>'storeid','title'=>'所属校区','type'=>'select','options'=>'stores'),
					array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
					array('name'=>'storetype','title'=>'分校类型','type'=>'radio','options'=>'storetype'),
					//array('name'=>'workerid','title'=>'','type'=>'select_more','options'=>'workers','option_title'=>'name'),
					array('name'=>'worker.name','title'=>'讲师','type'=>'string'),
					array('name'=>'onmonth','title'=>'上课月份','type'=>'string'),
					array('name'=>'onday','title'=>'上课时间','type'=>'string'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['storetype']=$this->storetype;
	$return['htmls']=$htmls;
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$workers=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	foreach($workers as &$wv){
		$wv['title']=$wv['name'];
		$wv['on_months']=$this->model('lesson')->get_months($wv['id']);
	}
	$return['workers2']=$workers;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['lessonid']){
		exi('请选择课程！','error');
	}
	if(!$postdata['storeid']){
		exi('请选择校区！','error');
	}
	if(!$postdata['workerid']){
		exi('请选择讲师！','error');
	}
	$data=array_fields(array('onmonth','onday','ontype','ontime','lessonid','area_id','workerid','storeid','storetype','person_limit','paixu','status'),$postdata);
	$id=intval($postdata['id']);
	$data['ontime']=strtotime($data['ontime']);
	$data['weekid']=date("w",$data['ontime']);
	if($id){
		pdo_update('jy_kexie_lesson_pai',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_kexie_lesson_pai',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$usid=$_SESSION['uniacid'];
	$searchs= ' a.uniacid ='.$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];

		$searchs.= ' and a.title like %'.trim($_RQ['title']).'%';
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];

		$searchs.= ' and a.area_id ='.intval($_RQ['area_id']).'';
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];

		$searchs.= ' and a.storeid ='.intval($_RQ['storeid']).'';
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];

		$searchs.= ' and a.groupid ='.intval($_RQ['groupid']).'';
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];

		$searchs.= ' and a.workerid ='.intval($_RQ['workerid']).'';
	}
	if(empty($_RQ['year'])){
		$year = date('Y');
	}else{
		$year=$_RQ['year'];
	}
	if($_RQ['ontime']){
		$time_range=cfc('tools')->base64_decode($_RQ['ontime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['ontime']=$time_range;
		$params['ontime between']=array($time_range['tstart'],$time_range['tend']);
		$searchs.= ' and a.ontime  BETWEEN '.$time_range['tstart'].'  AND '.$time_range['tend'].'' ;

	}else{
		$yeartime=strtotime($year.'-01');
		$lastyear=$year+1;
		$lasttime=strtotime($lastyear.'-01');
		$params['ontime between']=array($yeartime,$lasttime);

		$searchs.= ' and a.ontime  BETWEEN '.$yeartime.'  AND ' .$lasttime.'' ;
		 
	}
	if(!$_RQ['area_type'] && !$_RQ['worker_type'] && !$_RQ['store_type']){
	//	$lesson=pdo_getall('jy_kexie_lesson_pai',$params);
		//$lesson=pdo_fetchall('SELECT a.* FROM '.tablename('jy_kexie_lesson_pai').'a ');
		 $lesson=pdo_fetchall('SELECT a.* FROM '.tablename('jy_kexie_lesson_pai').'  a ' . ' left join ' . tablename('jy_kexie_worker') . 'm on a.workerid=m.id ' . ' left join ' . tablename('jy_kexie_store') . 'n on a.storeid=n.id ' . '  WHERE  '.$searchs .' and    m.status=1 and n.status=1 order by a.id desc');
		foreach($lesson as &$v){
			$v['starttime']=date('Y-m-d',$v['starttime']);
			$v['endtime']=date('Y-m-d',$v['endtime']);
			$store=pdo_get('jy_kexie_store',array('id'=>$v['storeid']));
			$v['storetypetitle']=$this->storetype[$store['type']];
			$v['storetitle']=$store['title'];
			$v['storelianxiname']=$store['lianxiname'];
			$v['storemobile']=$store['mobile'];
			$v['storetel']=$store['tel'];
			$v['storeaddress']=$store['address'];
			$v['storexianlu']=$store['xianlu'];
			$v['storetitle']=$v['storetitle']?:'无';
			$v['areatitle']=pdo_getcolumn('jy_kexie_area',array('id'=>$v['area_id']),'title');
			$v['grouptitle']=pdo_getcolumn('jy_kexie_lesson_group',array('id'=>$v['groupid']),'title');
			$worker=pdo_get('jy_kexie_worker',array('id'=>$v['workerid']));
			$v['workertitle']=$worker['name'];
			$v['workermobile']=$worker['mobile'];
			$v['year']=date('Y',$v['ontime']);
			$v['month']=date('m',$v['ontime']);
			$v['day']=date('d',$v['ontime']);
			$v['storenum']=pdo_count('jy_kexie_user',array('storeid'=>$v['storeid']),'id');
			$v['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$v['lessonid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$basic=$this->plugin_setting('basic');
	$basic['on_months']=str_array($basic['on_months']);
	foreach($basic['on_months'] as $ov){
		$on_months[]=array('month'=>$ov);
	}
	paixu($on_months,'month','asc');
	$weeks=array('周日','周一','周二','周三','周四','周五','周六');
	if($_RQ['area_type']){
		$areas=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
		$excel=array();
		$area_tops=array();
		foreach($areas as $av){
			$top=array('year'=>'年份','month'=>'月份');
			$workers=array();
			$stores=pdo_getall('jy_kexie_store',array('area_id'=>$av['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
			foreach($stores as $sv){
				$top['store_'.$sv['id']]=$sv['title'].'('.$av['title'].')';
			}
			$top['stores']=$stores;
			$area_tops[$av['id']]=$top;
		}
		foreach($area_tops as $ai=>$av){
			$stores=$av['stores'];
			unset($av['stores']);
			$excel[$ai]['header']=$av;
			$lesson=array();
			foreach($on_months as $k=>$ov){
				$nowkey=$ai.'_'.$k;
				$lesson[$nowkey]=array(
						'year'=>$year,
						'month'=>$ov['month'],
				);
				foreach($stores as $sv){
					$params=array('onday like'=>'%'.$year.'%','onmonth'=>$ov['month'],'storeid'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']);
					$pais=pdo_getall('jy_kexie_lesson_pai',$params,array('id','workerid','onday'));
					$workers=array();
					foreach($pais as $pv){
						$name=pdo_getcolumn('jy_kexie_worker',array('id'=>$pv['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
						$workers[$pv['workerid']]['name']=$name;
						if($workers[$pv['workerid']]['num']){
							$workers[$pv['workerid']]['num']++;
						}else{
							$workers[$pv['workerid']]['num']=1;
						}
						$onday=substr($pv['onday'],-2);
						$weekid=date("w",strtotime($pv['onday']));
						$weektitle=$weeks[$weekid];
					//	$onday='['.$onday.$weektitle.']';
						//$onday='['.$onday.']';
						
						
						if($workers[$pv['workerid']]['onday']){
							$workers[$pv['workerid']]['onday'].=$onday.'、';
						}else{
							$workers[$pv['workerid']]['onday']=$onday.'、';
						}
					}
					$data='';
					foreach($workers as $wv){
						$data .=$wv['name'].'（'.$wv['onday'].'）、';
					}
					$data=rtrim($data, "、");
					$lesson[$nowkey]['store_'.$sv['id']]=$data;
				}
			}
			$excel[$ai]['data']=$lesson;
		}
		$csvtitle='按区域导出';
		$this->model('model')->export2excel($excel,$csvtitle.'排课表');
		exit;
	}
	if($_RQ['store_type']){
		$areas=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
		$excel=array();
		foreach($areas as $ai=>$av){
			$workers=array();
			$stores=pdo_getall('jy_kexie_store',array('area_id'=>$av['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
			foreach($stores as $sv){
				$excel[$sv['id']]['header']=array(
					'year'=>'年份',
					'month'=>'月份',
					'store'=>$sv['title'].'('.$av['title'].')'
				);
				$lesson=array();
				foreach($on_months as $k=>$ov){
					$params=array('onday like'=>'%'.$year.'%','onmonth'=>$ov['month'],'storeid'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']);
					$pais=pdo_getall('jy_kexie_lesson_pai',$params,array('id','workerid','onday'));
					$workers=array();
					foreach($pais as $pv){
						$name=pdo_getcolumn('jy_kexie_worker',array('id'=>$pv['workerid'],'uniacid'=>$_SESSION['uniacid']),'name');
						$workers[$pv['workerid']]['name']=$name;
						if($workers[$pv['workerid']]['num']){
							$workers[$pv['workerid']]['num']++;
						}else{
							$workers[$pv['workerid']]['num']=1;
						}
						$onday=substr($pv['onday'],-2);
						$weekid=date("w",strtotime($pv['onday']));
						$weektitle=$weeks[$weekid];
						//$onday='['.$onday.$weektitle.']';
						//$onday='['.$onday.']';
						if($workers[$pv['workerid']]['onday']){
							$workers[$pv['workerid']]['onday'].=$onday.'、';
						}else{
							$workers[$pv['workerid']]['onday']=$onday.'、';
						}
					}
					$data='';
					foreach($workers as $wv){
						$data .=$wv['name'].'（'.$wv['onday'].'）、';
					}
					$data=rtrim($data, "、");
					$lesson[]=array(
							'year'=>$year,
							'month'=>$ov['month'],
							'store'=>$data,
					);
				}
				$excel[$sv['id']]['data']=$lesson;
			}
		}
		$csvtitle='按学校汇总导出';
		$this->model('model')->export2excel($excel,$csvtitle.'排课表');
		exit;
	}
	if($_RQ['store_type2']){
		$header=array(
			'storetitle'=>'学校',
			'storeaddress'=>'学校地址',
			'storexianlu'=>'乘车路线',
			'storemobile'=>'手机',
			'storetel'=>'固定电话',
			'areatitle'=>'区域',
			'year'=>'年',
			'month'=>'月',
			'day'=>'日',
			'title'=>'课程名称',
			'workertitle'=>'讲师',
			'storenum'=>'应到人数',
			'tingnum'=>'实到人数',
		);
		$csvtitle='按学校列表导出';
	}
	if($_RQ['worker_type']){
		$excel=array();
		$worker=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'));
		foreach($worker as $wv){
				$excel[$wv['id']]['header']=array(
					'year'=>'年份',
					'month'=>'月份',
					'worker'=>$wv['name']
				);
				$lesson=array();
				foreach($on_months as $k=>$ov){
					$params=array('onday like'=>'%'.$year.'%','onmonth'=>$ov['month'],'workerid'=>$wv['id'],'uniacid'=>$_SESSION['uniacid']);
					$pais=pdo_getall('jy_kexie_lesson_pai',$params,array('id','storeid','onday'));
					$stores=array();
					foreach($pais as $pv){
						$title=pdo_getcolumn('jy_kexie_store',array('id'=>$pv['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
						$stores[$pv['workerid']]['title']=$title;
						$onday=substr($pv['onday'],-2);
						$weekid=date("w",strtotime($pv['onday']));
						$weektitle=$weeks[$weekid];
						//$onday='['.$onday.$weektitle.']';
						//$onday='['.$onday.']';
						if($stores[$pv['workerid']]['onday']){
							$stores[$pv['workerid']]['onday'].=$title.'（'.$onday.'）';
						}else{
							$stores[$pv['workerid']]['onday']=$title.'（'.$onday.'）';
						}
					}
					$data='';
					foreach($stores as $sv){
						$data .=$sv['onday'];
					}
					$data=rtrim($data, "、");
					$lesson[]=array(
							'year'=>$year,
							'month'=>$ov['month'],
							'worker'=>$data,
					);
				}
				$excel[$wv['id']]['data']=$lesson;
		}
		$csvtitle='按教师导出';
		$this->model('model')->export2excel($excel,$csvtitle.'排课表');
		exit;
	}
	if(!$header){
		$header=array(
			'year'=>'年',
			'month'=>'月',
			'day'=>'日',
			'areatitle'=>'区域',
			'storetitle'=>'学校',
			'storetypetitle'=>'学校类型',
			'storelianxiname'=>'学校负责人',
			'storemobile'=>'手机',
			'storetel'=>'固定电话',
			'storeaddress'=>'地址',
			'storexianlu'=>'乘车路线',
			'workertitle'=>'讲师',
			'grouptitle'=>'课程分类',
			'lessontitle'=>'课程名称',
			'workermobile'=>'讲师电话',
			'storenum'=>'应到人数',
			'tingnum'=>'实到人数',
		);
	}
	cfc('tools')->export2excel($header,$lesson,$csvtitle.'排课表');
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item=pdo_get('jy_kexie_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
		unset($item['password']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look'=>array(
					array('name'=>'headimg','title'=>'头像','type'=>'image'),
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'tel','title'=>'固定电话','type'=>'string'),
					array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
					array('name'=>'years','title'=>'出生年月','type'=>'time_date'),
					array('type'=>'checkbox','title'=>'授课时间','name'=>'on_days','help'=>'','options'=>'on_days'),
					array('name'=>'on_num','title'=>'上课次数','type'=>'string'),
					array('name'=>'address','title'=>'现居住地','type'=>'string'),
					array('name'=>'show','title'=>'讲课内容','type'=>'text'),
					array('name'=>'desc','title'=>'教师简介','type'=>'richtext'),
					array('name'=>'lessondesc','title'=>'课程简介','type'=>'richtext'),
					array('name'=>'remark','title'=>'备注','type'=>'text'),
		)
	);
	$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']));
	$return['lesson']=pdo_getall('jy_kexie_lesson_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='up_shenhe'){
	pdo_update('jy_kexie_lesson_pai',array('status'=>1),array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
	exi('审核成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的课程！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_lesson_pai',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}